﻿using AutoMapper;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;
using T_Rbac_APi.Application.Command.Operator;
using T_Rbac_APi.Dto;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.OperatorHandler
{
    public class OperatorAddHandlerCommand : IRequestHandler<OperatorAddCommand, int>
    {
        private readonly IOperator ioperator;
        private readonly IMapper mapper;
        private readonly IjobRep jobRep;
        private readonly Cross_services_Helper cross_Services_Helper;

        public OperatorAddHandlerCommand(IOperator ioperator, IMapper mapper, Cross_services_Helper cross_Services_Helper, IjobRep jobRep)
        {
            this.ioperator = ioperator;
            this.mapper = mapper;
            this.cross_Services_Helper = cross_Services_Helper;
            this.jobRep = jobRep;
        }

        public async Task<int> Handle(OperatorAddCommand request, CancellationToken cancellationToken)
        {
            request.CreateDate = DateTime.Now;
            request.CreateById = 0;
            request.CreateBy = "小明";
            var listaa = await ioperator.GetAloneByresult(x => x.Oname == request.Oname);
            var list22 = await ioperator.GetAloneByresult(x => x.Onumber == request.Onumber);
            if (listaa!=null)
            {
                return await Task.FromResult(-2);
            }
            if (list22!=null)
            {
                return await Task.FromResult(-3);
            }
            var lists = await jobRep.GetAloneByid(request.Orid);
            var lista = await cross_Services_Helper.Get($"http://10.31.56.10:8000/employeeread/api/EmployeeReadApI/GetEmployeeByname?name={request.Oname}");
            var words = JsonConvert.DeserializeObject<PublicArray>(lista);
            request.Ophone = words.phone;
            request.Odname = words.dname;
            request.Orname=lists.Jname;
            var list = mapper.Map<Operator>(request);
            return await ioperator.Add(list);

        }
    }
}
